package com.example.ql.express.utils.operate;

import com.example.ql.express.model.constants.FieldConstant;
import com.example.ql.express.model.constants.SystemCharConstant;
import com.example.ql.express.model.enums.AppendRuleEnum;
import com.example.ql.express.model.vo.ScriptConfigVO;
import com.example.ql.express.utils.CommonAppendUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * todo 搜词和末次转换问题
 *
 * @author xuweizhi
 * @since 2021/10/28 10:55
 */
public class VitalSignOperateCompare implements OperateCompare {

    /**
     * 外层对象参数校验
     */
    private final static List<String> VALIDATE_RULE = Arrays.asList(
            String.format(SystemCharConstant.NULL_CONSTANT, FieldConstant.VITAL_SIGN),
            String.format(SystemCharConstant.NULL_CONSTANT, FieldConstant.DISEASE),
            String.format(SystemCharConstant.NULL_CONSTANT, FieldConstant.DISEASE_PATIENT_TYPE)
    );

    @Override
    public String generateCompareRule(ScriptConfigVO scriptConfig) {
        StringBuilder stringBuilder = new StringBuilder();
        if (Objects.equals(scriptConfig.getAppendRule(), AppendRuleEnum.FIRST) || Objects.equals(scriptConfig.getAppendRule(), AppendRuleEnum.ALL)) {
            stringBuilder.append(String.format(AppendRuleEnum.FIRST.getContent(), CommonAppendUtils.nullPointAppend(VALIDATE_RULE, true)));
        }
        stringBuilder.append(scriptConfig.getReservedTable()).append(String.format(SystemCharConstant.ONE_3, CommonAppendUtils.nullPointAppend(scriptConfig.getValidateRule(), true)));
        String tableOne = scriptConfig.getReservedTable() + SystemCharConstant.TABLE;
        stringBuilder.append(tableOne).append(String.format(SystemCharConstant.ONE_3, scriptConfig.getCompareRule()));
        String tableTwo = tableOne + SystemCharConstant.TABLE;
        stringBuilder.append(tableTwo).append("value = Math.min(value,").append(scriptConfig.getLevels().get(0)).append(");\n");
        stringBuilder.append(tableOne).append(SystemCharConstant.RIGHT);
        stringBuilder.append(scriptConfig.getReservedTable()).append(SystemCharConstant.RIGHT);
        if (Objects.equals(scriptConfig.getAppendRule(), AppendRuleEnum.END) || Objects.equals(scriptConfig.getAppendRule(), AppendRuleEnum.ALL)) {
            stringBuilder.append(AppendRuleEnum.END.getContent());
        }
        return stringBuilder.toString();
    }
}
